package cn.dlust.tt.module.teacher.controller.admin.lessonplans;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.dlust.tt.framework.common.pojo.PageParam;
import cn.dlust.tt.framework.common.pojo.PageResult;
import cn.dlust.tt.framework.common.pojo.CommonResult;
import cn.dlust.tt.framework.common.util.object.BeanUtils;
import static cn.dlust.tt.framework.common.pojo.CommonResult.success;

import cn.dlust.tt.framework.excel.core.util.ExcelUtils;

import cn.dlust.tt.framework.apilog.core.annotation.ApiAccessLog;
import static cn.dlust.tt.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.dlust.tt.module.teacher.controller.admin.lessonplans.vo.*;
import cn.dlust.tt.module.teacher.dal.dataobject.lessonplans.LessonPlansDO;
import cn.dlust.tt.module.teacher.service.lessonplans.LessonPlansService;

@Tag(name = "管理后台 - 教案")
@RestController
@RequestMapping("/teacher/lesson-plans")
@Validated
public class LessonPlansController {

    @Resource
    private LessonPlansService lessonPlansService;

    @PostMapping("/create")
    @Operation(summary = "创建教案")
    @PreAuthorize("@ss.hasPermission('teacher:lesson-plans:create')")
    public CommonResult<Long> createLessonPlans(@Valid @RequestBody LessonPlansSaveReqVO createReqVO) {
        return success(lessonPlansService.createLessonPlans(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新教案")
    @PreAuthorize("@ss.hasPermission('teacher:lesson-plans:update')")
    public CommonResult<Boolean> updateLessonPlans(@Valid @RequestBody LessonPlansSaveReqVO updateReqVO) {
        lessonPlansService.updateLessonPlans(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除教案")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('teacher:lesson-plans:delete')")
    public CommonResult<Boolean> deleteLessonPlans(@RequestParam("id") Long id) {
        lessonPlansService.deleteLessonPlans(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得教案")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('teacher:lesson-plans:query')")
    public CommonResult<LessonPlansRespVO> getLessonPlans(@RequestParam("id") Long id) {
        LessonPlansDO lessonPlans = lessonPlansService.getLessonPlans(id);
        return success(BeanUtils.toBean(lessonPlans, LessonPlansRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得教案分页")
    @PreAuthorize("@ss.hasPermission('teacher:lesson-plans:query')")
    public CommonResult<PageResult<LessonPlansRespVO>> getLessonPlansPage(@Valid LessonPlansPageReqVO pageReqVO) {
        PageResult<LessonPlansDO> pageResult = lessonPlansService.getLessonPlansPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LessonPlansRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出教案 Excel")
    @PreAuthorize("@ss.hasPermission('teacher:lesson-plans:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLessonPlansExcel(@Valid LessonPlansPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LessonPlansDO> list = lessonPlansService.getLessonPlansPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "教案.xls", "数据", LessonPlansRespVO.class,
                        BeanUtils.toBean(list, LessonPlansRespVO.class));
    }

}